﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.RealTimeSimulation.CApRealTimeSimulationApp
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Threading;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.RealTimeSimulation;

internal class CApRealTimeSimulationApp
{
  private int m_firstRealTime = -1;
  private double m_firstSimTime = -1.0;
  private bool m_isActive;
  private bool m_isBackwards;

  public void SimulationPlayer_TimeIntervalReached(
    object sender,
    TxSimulationPlayer_TimeIntervalReachedEventArgs args)
  {
    this.WaitUntilRealTimePassed(args.CurrentTime);
  }

  public void SimulationPlayer_SimulationStartedForward(
    object sender,
    TxSimulationPlayer_SimulationStartedForwardEventArgs args)
  {
    this.m_isActive = true;
    this.m_isBackwards = false;
    this.InitCounters();
  }

  public void SimulationPlayer_SimulationStartedBackward(
    object sender,
    TxSimulationPlayer_SimulationStartedBackwardEventArgs args)
  {
    this.m_isActive = true;
    this.m_isBackwards = true;
    this.InitCounters();
  }

  private void InitCounters()
  {
    this.m_firstRealTime = Environment.TickCount;
    this.m_firstSimTime = TxApplication.ActiveDocument.SimulationPlayer.CurrentTime;
  }

  public void SimulationPlayer_SimulationStopped(
    object sender,
    TxSimulationPlayer_SimulationStoppedEventArgs args)
  {
    this.m_isActive = false;
  }

  public void WaitUntilRealTimePassed(double simTime)
  {
    if (!this.m_isActive || !TxApplication.ActiveDocument.SimulationPlayer.RefreshMode)
      return;
    int num = Environment.TickCount - this.m_firstRealTime;
    int int32 = Convert.ToInt32((this.m_isBackwards ? this.m_firstSimTime - simTime : simTime - this.m_firstSimTime) * 1000.0);
    if (int32 > num)
      Thread.Sleep(int32 - num);
    else
      this.m_firstRealTime += num - int32;
  }

  public void Start()
  {
    this.RegisterToEvents();
    if (TxApplication.ActiveDocument.SimulationPlayer.IsSimulationRunning())
      this.InitCounters();
    this.m_isActive = true;
  }

  public void Stop()
  {
    this.m_isActive = false;
    this.UnregisterFromEvents();
  }

  private void RegisterToEvents()
  {
    TxApplication.ActiveDocument.SimulationPlayer.TimeIntervalReached += new TxSimulationPlayer_TimeIntervalReachedEventHandler(this.SimulationPlayer_TimeIntervalReached);
    TxApplication.ActiveDocument.SimulationPlayer.SimulationStartedForward += new TxSimulationPlayer_SimulationStartedForwardEventHandler(this.SimulationPlayer_SimulationStartedForward);
    TxApplication.ActiveDocument.SimulationPlayer.SimulationStartedBackward += new TxSimulationPlayer_SimulationStartedBackwardEventHandler(this.SimulationPlayer_SimulationStartedBackward);
    TxApplication.ActiveDocument.SimulationPlayer.SimulationStopped += new TxSimulationPlayer_SimulationStoppedEventHandler(this.SimulationPlayer_SimulationStopped);
  }

  private void UnregisterFromEvents()
  {
    TxApplication.ActiveDocument.SimulationPlayer.TimeIntervalReached -= new TxSimulationPlayer_TimeIntervalReachedEventHandler(this.SimulationPlayer_TimeIntervalReached);
    TxApplication.ActiveDocument.SimulationPlayer.SimulationStartedForward -= new TxSimulationPlayer_SimulationStartedForwardEventHandler(this.SimulationPlayer_SimulationStartedForward);
    TxApplication.ActiveDocument.SimulationPlayer.SimulationStartedBackward -= new TxSimulationPlayer_SimulationStartedBackwardEventHandler(this.SimulationPlayer_SimulationStartedBackward);
    TxApplication.ActiveDocument.SimulationPlayer.SimulationStopped -= new TxSimulationPlayer_SimulationStoppedEventHandler(this.SimulationPlayer_SimulationStopped);
  }
}
